package com.metrics.reports;

import java.io.Serializable;
import java.util.*;

import com.database.daos.IStringDao;
import com.database.objects.*;
import com.metrics.extractors.MetricExtractor;

public class ReportGenerator implements Serializable {

	private static final long serialVersionUID = 2317431673245094129L;

	public static final String SEPARATOR = "$";

	private IStringDao stringDao;

	public List<String> generate(Project project, Repository repository, MetricExtractor extractor) {
		List<String> lines = new ArrayList<String>();
		printTitles(repository, lines);

		for (Developer developer : project.getDevelopers()) {
			List<String> dates = stringDao.findDatesContributionByDeveloper(developer);
			for (String date : dates) {
				String line = developer.getName() + SEPARATOR;
				line += date + SEPARATOR;
				for (Type type : repository.getTypes()) {
					for (Status status : repository.getStatus()) {
						Double value = extractor.extract(developer, date, type, status);
						line += value + SEPARATOR;
					}
				}
				lines.add(line.substring(0, line.length() - 1));
			}
		}
		return lines;
	}

	private void printTitles(Repository repository, List<String> lines) {
		String header = "DEVELOPER" + SEPARATOR;
		header += "DATE" + SEPARATOR;
		for (Type type : repository.getTypes()) {
			for (Status status : repository.getStatus()) {
				header += type + "_" + status + SEPARATOR;
			}
		}
		lines.add(header.substring(0, header.length() - 1));
	}

	public void setStringDao(IStringDao stringDao) {
		this.stringDao = stringDao;
	}

}